package com.tanhua.admin.module.dynamicmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.admin.module.dynamicmanage.service.TopMessageService;
import com.tanhua.common.mapper.PublishInfoMapper;
import com.tanhua.common.pojo.PublishInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class TopMessageServiceImpl implements TopMessageService {

    @Autowired
    private PublishInfoMapper publishInfoMapper;


    /**
     * 消息置顶
     * @param id
     * @return
     */
    @Override
    public Map<String, String> topState(String id) {
        //更新字段 置顶后 变为2
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setTopState(2);
        System.out.println(publishInfo);
        //构建条件
        QueryWrapper<PublishInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("publish_id", id);

        //更新数据库
        try {
            int update = publishInfoMapper.update(publishInfo, wrapper);
            if (update == 1) {
                //更新成功
                Map<String,String> resultMap = new HashMap<>();
                resultMap.put("message","操作成功");
                return resultMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 消息取消置顶
     * @param id
     * @return
     */
    @Override
    public Map<String, String> unTopState(String id) {
        //更新字段 取消置顶后 变为1
        PublishInfo publishInfo = new PublishInfo();
        publishInfo.setTopState(1);
        System.out.println(publishInfo);
        //构建条件
        QueryWrapper<PublishInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("publish_id", id);

        //更新数据库
        try {
            int update = publishInfoMapper.update(publishInfo, wrapper);
            if (update == 1) {
                //更新成功
                Map<String,String> resultMap = new HashMap<>();
                resultMap.put("message","操作成功");
                return resultMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
